package cn.edu.gcu.giftems.service;

import cn.edu.gcu.giftems.daos.StrategyDao;
import cn.edu.gcu.giftems.pojos.Strategy;
import cn.edu.gcu.giftems.utils.PageUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
@Transactional
public class StrategyService {

    private StrategyDao strategyDao = new StrategyDao();

    public StrategyDao getStrategyDao() {
        return strategyDao;
    }

    public void setStrategyDao(StrategyDao strategyDao) {
        this.strategyDao = strategyDao;
    }

    public void save(Strategy strategy) {
        strategyDao.save(strategy);
    }

    //根据攻略编号查询攻略信息
    public Strategy findById(int id) {
        return strategyDao.finById(id);
    }

    //更新攻略信息
    public void update(Strategy strategy) {
        strategyDao.update(strategy);
    }

    //根据攻略id删除攻略
    public void delete(Strategy strategy) {
        strategyDao.delete(strategy);
    }

    public int findCount() {
        return strategyDao.findCount();
    }


    //分页查询
    public PageUtils<Strategy> findByPage(int currPage) {
        PageUtils<Strategy> PageUtils = new PageUtils<Strategy>();
        //封装当前的页数
        PageUtils.setCurrPage(currPage);
        //封装每页记录数
        int pageSize = 10;
        PageUtils.setPageSize(pageSize);
        //封装总记录数
        int totalCount = strategyDao.findCount();
        PageUtils.setTotalCount(totalCount);
        //封装总页数
        int totalPage = totalCount%pageSize == 0?totalCount/pageSize:(totalCount/pageSize+1);
        PageUtils.setTotalPage(totalPage);
        //封装每页显示的数据
        int begin = (currPage - 1)*pageSize;
        List<Strategy> list = strategyDao.findByPage(begin,pageSize);
        PageUtils.setList(list);
        return PageUtils;
    }

    public List<Strategy> findAll() {
        return strategyDao.findAll();
    }

    public PageUtils<Strategy> findByParameter(int currPage, String filter_name, String simple_filter_value) {
        PageUtils<Strategy> PageUtils = new PageUtils<Strategy>();
        PageUtils.setCurrPage(currPage);
        PageUtils.setTotalPage(1);
        List<Strategy> list = strategyDao.findByParameter(filter_name,simple_filter_value);
        PageUtils.setList(list);
        int totalCount = list.size();
        PageUtils.setTotalCount(totalCount);
        return PageUtils;
    }
}
